<?php

namespace app\common\model;

use think\Model;

class Admin extends Model
{
    public static $user_status_category = array(0 => '正常', 1 => '冻结', 2 => '已删除');
    public function getUserStatusAttr($value)
    {
        $data = self::$user_status_category;

        return $data[$value];
    }
    public function setUserStatusAttr($value)
    {
        $data = self::$user_status_category;
        $data = array_flip($data);
        return $data[$value];
    }


    private function password_hash($password, $salt)
    {
        return hash('sha256', $password.$salt);
    }
    public function setPasswordAttr($value, $data)
    {
        if (empty($this->salt)) {
            $this->salt = $this->getSalt();
        }

        return $this->password_hash($value, $this->data['salt']);
    }

    /**
     * 生成盐（随机字符串）.
     *
     * @return string 盐值
     */
    protected static function getSalt()
    {
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $noncestr = '';
        for ($i = 0; $i < 16; ++$i) {
            $noncestr .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }

        return $noncestr;
    }

    /**
     * 检测密码
     *
     * @param string $value 密码
     *
     * @return bool 此密码是否正确
     */
    public function checkPassword($value)
    {

        return $this->password == $this->password_hash($value, $this->salt);
    }

}